/*            Copyright(c) 2006-2008 Dave Sexton           *
 * http://www.codeplex.com/DocProject                      *
 * http://www.codeplex.com/DocProject/Project/License.aspx *
 ***********************************************************/
using System;
using System.Collections.Generic;
using System.Text;
using System.ComponentModel;

namespace DaveSexton.DocProject
{
	public sealed class BooleanStringConverter : BooleanConverter
	{
		#region Public Properties
		#endregion

		#region Private / Protected
		#endregion

		#region Constructors
		/// <summary>
		/// Constructs a new instance of the <see cref="BooleanStringConverter" /> class.
		/// </summary>
		public BooleanStringConverter()
		{
		}
		#endregion

		#region Methods
		private static bool TryGetBooleanValues(ITypeDescriptorContext context, out BooleanValuesAttribute attribute)
		{
			if (context != null)
			{
				PropertyDescriptor property = context.PropertyDescriptor;

				if (property != null)
				{
					attribute = property.Attributes[typeof(BooleanValuesAttribute)] as BooleanValuesAttribute;
					return true;
				}
			}

			attribute = null;
			return false;
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
		public override object ConvertFrom(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value)
		{
			BooleanValuesAttribute attribute;
			if (value is string && TryGetBooleanValues(context, out attribute))
			{
				string s = (string) value;

				if (string.Equals(s, attribute.TrueValue, StringComparison.OrdinalIgnoreCase))
					return true;
				else if (string.Equals(s, attribute.FalseValue, StringComparison.OrdinalIgnoreCase))
					return false;
			}

			return base.ConvertFrom(context, culture, value);
		}

		public override object ConvertTo(ITypeDescriptorContext context, System.Globalization.CultureInfo culture, object value, Type destinationType)
		{
			BooleanValuesAttribute attribute;
			if (value is bool && destinationType == typeof(string) && TryGetBooleanValues(context, out attribute))
			{
				if ((bool) value)
					return attribute.TrueValue;
				else
					return attribute.FalseValue;
			}

			return base.ConvertTo(context, culture, value, destinationType);
		}

		public override TypeConverter.StandardValuesCollection GetStandardValues(ITypeDescriptorContext context)
		{
			BooleanValuesAttribute attribute;
			if (TryGetBooleanValues(context, out attribute))
				return new StandardValuesCollection(
					new string[] { attribute.TrueValue, attribute.FalseValue });
			else
				return base.GetStandardValues(context);
		}

		[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Performance", "CA1800:DoNotCastUnnecessarily")]
		public override bool IsValid(ITypeDescriptorContext context, object value)
		{
			if (value is bool)
				return true;
			else if (value is string)
			{
				string s = (string) value;

				BooleanValuesAttribute attribute;
				if (TryGetBooleanValues(context, out attribute))
					return string.Equals(s, attribute.TrueValue, StringComparison.OrdinalIgnoreCase)
						|| string.Equals(s, attribute.FalseValue, StringComparison.OrdinalIgnoreCase);
			}

			return false;
		}
		#endregion
	}
}
